﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;

namespace GPM
{
    public partial class CPTreeView : UserControl
    {
        private TreeListNode hotTrackNode = null;

        private TreeListNode HotTrackNode
        {
            get { return hotTrackNode; }
            set
            {
                if (hotTrackNode != value)
                {
                    TreeListNode prevHotTrackNode = hotTrackNode;
                    hotTrackNode = value;
                    if (treeView.ActiveEditor != null)
                        treeView.PostEditor();
                    treeView.RefreshNode(prevHotTrackNode);
                    treeView.RefreshNode(hotTrackNode);
                }
            }
        }

        public CPTreeView()
        {
            InitializeComponent();
        }

        private void barSelectAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //splashScreenManager1.ShowWaitForm();
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm1));
            if (((DevExpress.XtraBars.BarButtonItem)e.Item).Down)
            {
                treeView.CheckAll();
                //foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in treeView.Nodes)
                //{
                //    node.Checked = true;
                //    SetCheckedChildNodes(node, node.CheckState);
                //}
            }
            else
            {
                treeView.UncheckAll();
                //foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in treeView.Nodes)
                //{
                //    node.Checked = false;
                //    SetCheckedChildNodes(node, node.CheckState);
                //}
            }
            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
            //splashScreenManager1.CloseWaitForm();
        }

        private void barExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (((DevExpress.XtraBars.BarButtonItem)e.Item).Down)
            {
                treeView.ExpandAll();
            }
            else
            {
                treeView.CollapseAll();
            }
        }

        private void barFilter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (((DevExpress.XtraBars.BarButtonItem)e.Item).Down)
            {
                treeView.OptionsView.ShowAutoFilterRow = true;
            }
            else
            {
                treeView.OptionsView.ShowAutoFilterRow = false;
            }
        }

        private void barShowSummary_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (((DevExpress.XtraBars.BarButtonItem)e.Item).Down)
            {
                treeView.OptionsView.ShowSummaryFooter = true;
            }
            else
            {
                treeView.OptionsView.ShowSummaryFooter = false;
            }
        }

        /// <summary>
        /// 设置子节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }

        /// <summary>
        /// 设置父节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }

        private void treeView_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            //SetCheckedChildNodes(e.Node, e.Node.CheckState);
            //SetCheckedParentNodes(e.Node, e.Node.CheckState);
        }

        private void treeView_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            //e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }

        public event DevExpress.XtraBars.ItemClickEventHandler Refresh_ItemClick;
        private void barRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        { 
            if (null != Refresh_ItemClick)
                Refresh_ItemClick(sender, e); 
        }

        private void treeView_FilterNode(object sender, DevExpress.XtraTreeList.FilterNodeEventArgs e)
        {
            List<TreeListColumn> filteredColumns = e.Node.TreeList.Columns.Cast<TreeListColumn>(
                ).Where(c => c.FilterInfo.AutoFilterRowValue != null).ToList();
            if (filteredColumns.Count == 0) return;
            e.Handled = true;

            if ((null != e.Node.ParentNode) && (e.Node.ParentNode.Visible == true))
                e.Node.Visible = true;
            else
                e.Node.Visible = filteredColumns.Any(c => IsNodeMatchFilter(e.Node, c));
        }

        static bool IsNodeMatchFilter(TreeListNode node, TreeListColumn column)
        {
            string filterValue = column.FilterInfo.AutoFilterRowValue.ToString();
            if (node.GetDisplayText(column).Contains(filterValue)) return true;
            foreach (TreeListNode n in node.Nodes)
                if (IsNodeMatchFilter(n, column)) return true;
            return false;
        }

        private void treeView_NodeCellStyle(object sender, DevExpress.XtraTreeList.GetCustomNodeCellStyleEventArgs e)
        {
            if (e.Node == HotTrackNode)
                e.Appearance.BackColor = Color.LightBlue;
        }

        private void treeView_MouseMove(object sender, MouseEventArgs e)
        {
            TreeList treelist = sender as TreeList;
            TreeListHitInfo info = treelist.CalcHitInfo(new Point(e.X, e.Y));
            HotTrackNode = info.HitInfoType == HitInfoType.Cell ? info.Node : null;
        }

        private void treeView_MouseLeave(object sender, EventArgs e)
        {
            HotTrackNode = null;
        }

    }
}
